package py.edu.ucsa.sisalews.services.rest.impl;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import py.edu.ucsa.sisalews.config.SisalewsApplicationContextProvider;
import py.edu.ucsa.sisalews.model.services.impl.EmpresaServiceImpl;
import py.edu.ucsa.sisalews.persistence.dto.EmpresaDTO;
import py.edu.ucsa.sisalews.services.rest.EmpresaREST;

@Path("/empresa")
public class EmpresaRESTImpl implements EmpresaREST {
	
private ApplicationContext ctx;
	
	@Autowired
	private EmpresaServiceImpl empresaService;
	
	public EmpresaRESTImpl(){
		ctx = SisalewsApplicationContextProvider.getContext();
	}
	
	public void setEmpresaService(EmpresaServiceImpl empresaService) {
		this.empresaService = empresaService;
	}

	@Produces({"application/json"})
	@GET
	@Override
	public EmpresaDTO getEmpresa() {
		empresaService = (EmpresaServiceImpl) ctx.getBean("empresaService");
		return empresaService.getEmpresa();
	}
	
	@POST
	@Override
	public Response insertar(String empresa) {
		empresaService = (EmpresaServiceImpl) ctx.getBean("empresaService");
		String result = empresaService.insertar(empresa);
		return Response.ok(result).type(MediaType.APPLICATION_JSON).build();
	}

	@PUT
	@Override
	public Response modificar(String empresa) {
		empresaService = (EmpresaServiceImpl) ctx.getBean("empresaService");
		String result = empresaService.actualizar(empresa);
		return Response.ok(result).type(MediaType.APPLICATION_JSON).build();
	}

	@Path("/{ruc}")
	@DELETE
	@Override
	public Response eliminar(Integer ruc) {
		empresaService = (EmpresaServiceImpl) ctx.getBean("empresaService");
		empresaService.eliminar(ruc);
		return Response.ok().type(MediaType.APPLICATION_JSON).build();
	}

}
